<?php
	class FunctionTransaction{
		static function registerAccount($mailaddress,$password){
			$rs = new Result();
			if(strlen($mailaddress) > 0){
					if(preg_match("/[\w\d_]+@[.\w\d_]+/i", $mailaddress)){
						if(!FunctionData::checkEmailUsed($mailaddress)){
							$reguser = new DataUserRegister();
							$reguser->MailAddressBytes = unpack('C*', $mailaddress ) ;
							$reguser->PasswordHashBytes = unpack('C*', UtilHash::sha256( (strlen($password)>0)?$password:"" )  ) ;
							$reguser->SecretKeyBytes = unpack('C*', UtilHash::generateKey( 32 )  ) ;
							$confirm = DataWaitingConfirm::generate();
							{if(!isset( FunctionData::$static  ) ||  FunctionData::$static  ==null){ FunctionData::$static   = new Memcache; FunctionData::$static  ->connect("127.0.0.1" , 11215 ) or die ("Could not connect");};} ;
							{if( FunctionData::$static  ->get( "mail_reginfo_" .$mailaddress )==null) FunctionData::$static  ->set( "mail_reginfo_" .$mailaddress , $reguser->getBinary() );else  FunctionData::$static  ->replace( "mail_reginfo_" .$mailaddress , $reguser->getBinary() );} ;
							{if( FunctionData::$static  ->get( "mail_confirm_" .$mailaddress )==null) FunctionData::$static  ->set( "mail_confirm_" .$mailaddress , $confirm->getBinary() );else  FunctionData::$static  ->replace( "mail_confirm_" .$mailaddress , $confirm->getBinary() );} ;
							{if( FunctionData::$static  ->get( "mng_numof_user_registed"  )!=null) FunctionData::$static  ->increment( "mng_numof_user_registed"  );else  FunctionData::$static  ->set( "mng_numof_user_registed"  ,1);} ;
							{ FunctionData::$static  ->close(); FunctionData::$static   = null;} ;
							$rs->addData("code",$confirm->getCode());
							$mail = new PHPMailer();
							$mail->IsSMTP();
							$mail->SMTPAuth   = true;                  	 
							$mail->SMTPSecure = "ssl";                 	 
							$mail->Host       = "smtp.gmail.com";      	 
							$mail->Port       = 465;                   	 
							$mail->Username   = "admin@colorbox.com.vn" ;  	 
							$mail->Password   = "" ;             
							$mail->From       = "admin@colorbox.com.vn" ;
							$mail->FromName   = "Colorbox Social Network";
							$mail->Subject    = "Register Confirm";
							$mail->AltBody    = "This is the body when user views in plain text format";  
							$mail->WordWrap   = 50;  
							$ptn = readPattern($_SESSION[ "service_dir" ]  .'/pattern/' .'ptn_'. "registration_letter" .'.php' );
							$mail->MsgHTML( $ptn ->publish() );
							$mail->IsHTML(true);  
							if(!$mail->Send()){
								$rs->addError(4 , "Can not send confirm mail, please contact administrator, or request another confirm code.");
							}else{
								$rs->addData("messate","Register Successful, please check mail for confirm code. Your account must be confirm before using to login.");
							}
							$rs->return = true;
						}else{
							$rs->addError(3 ,$mailaddress." had been used for registed.");
						}
					}else{
						$rs->addError(1 , $mailaddress." not allow.");
					}
			}else{
				$rs->addError(1 , "Email address empty!");
			}
			return $rs;
		}
		static function confirmRegistration($mailaddress, $confirmcode){
			$rs = new Result();
			if(strlen($mailaddress) > 0){
				{if(!isset( FunctionData::$static  ) ||  FunctionData::$static  ==null){ FunctionData::$static   = new Memcache; FunctionData::$static  ->connect("127.0.0.1" , 11215 ) or die ("Could not connect");};} ;
				$confirm =  FunctionData::$static  ->get( "mail_confirm_" .$mailaddress ) ;
				if($confirm != null){
					$confirmobj = DataWaitingConfirm::swap(ByteBuffer::wrap( $confirm ) );
					if($confirmobj->getCode() == $confirmcode){
						$rs->return = true;
						$id = FunctionData::registerAnUserId();
						FunctionData::connectIdAndMailAddress($id,$mailaddress);
						 FunctionData::$static  ->delete( "mail_confirm_" .$mailaddress ) ;
						{if( FunctionData::$static  ->get( "mng_numof_user_confirm"  )!=null) FunctionData::$static  ->increment( "mng_numof_user_confirm"  );else  FunctionData::$static  ->set( "mng_numof_user_confirm"  ,1);} ;
					}else{
						$rs->return = false;
						$rs->addError(6 ,"Wrong code!");
					}
				}else{
					$rs->addError(5 , "Confirm error, please contact to Administrator");
				}
			}else{
				$rs->addError(1 ,"Email address empty!");
			}
			return $rs;
		}
		static function changePassword($old_password,$new_password){
			$rs = new Result();
			if(FunctionSession::isLogin()){
				$mail_address = FunctionSession::getEmailAddress();
				$account = FunctionData::readAccountRegisterInfo($mail_address);
				$old_hash = UtilHash::sha256( $old_password ) ;
				$new_hash = UtilHash::sha256( $new_password ) ;
				if($account->getPasswordHash() == $old_hash){
					$account->setPasswordHash($new_hash);
					FunctionData::writeAccountRegisterInfo($mail_address,$account);
					$rs->addData("Success","Your password has been changed.");
				}else{
					{Error::getInstance() ->addError( ERROR_WRONG_PASSWORD , "Wrong Password!" );} ;
				}
			}else{
				{Error::getInstance() ->addError( 17  , "Please login first!" );} ;
			}
			return $rs;
		}
		static function registerAGuest($guestid){
			if(FunctionData::checkGuestIDUsed($guestid)){
				return false;
			}else{
				$id = FunctionData::registerAnUserId();
				FunctionData::connectIdAndGuest($id,$guestid);
				$guestdata = new DataGuest($guestid);
				FunctionData::writeGuest($guestid,$guestdata);
				return true;
			}
		}
	}
?>